package com.android.whereismy;

import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.android.whereismy.util.CmnConsts;
import com.android.whereismy.util.CmnUtils;

/**
 * WhereIsMy main class of WhereIsMy project.
 * @author akarae
 *
 */
public class WhereIsMy extends Activity {
    
    private boolean isMobileCon = false;
    private boolean isWifiCon = false;
    
    private boolean isNetworkLocationActivated = false;
    private boolean isGpsLocationActivated = false;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.main);
        
        getSystemInfo();

        // Unit Button
        final Button button_unit = (Button) findViewById(R.id.button_unit);
        button_unit.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if (isMobileCon || isWifiCon) {
                    if (isGpsLocationActivated || isNetworkLocationActivated) {
                        startActivity(new Intent(getApplicationContext(),  Unit.class));
                    } else {
                        Toast.makeText(WhereIsMy.this, "check your location-service", Toast.LENGTH_LONG).show();
                    }
                    
                } else {
                    Toast.makeText(WhereIsMy.this, "check your connection", Toast.LENGTH_LONG).show();
                }
            }
        });
        
        // ControlCenter Button
        final Button button_controlCenter = (Button) findViewById(R.id.button_controlcenter);
        button_controlCenter.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if (isMobileCon || isWifiCon) {
                    if (isGpsLocationActivated || isNetworkLocationActivated) {
                        startActivity(new Intent(getApplicationContext(), Controller.class));
                    } else {
                        Toast.makeText(WhereIsMy.this, "check your location-service", Toast.LENGTH_LONG).show();
                    }
                } else {
                    Toast.makeText(WhereIsMy.this, "confirm your connection", Toast.LENGTH_LONG).show();
                }
            }
        });
        
        // Unit Button
        final Button button_help = (Button) findViewById(R.id.button_help);
        button_help.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                startActivity(new Intent(getApplicationContext(),  Help.class));
            }
        });        
    }
    
    @Override
    public void onRestart() {
        super.onRestart();
        getSystemInfo();
    }
    
    protected void getSystemInfo() {

        TextView mobileStatTv = (TextView) findViewById(R.id.text_mobile_stat);
        TextView mobileConStatTv = (TextView) findViewById(R.id.text_mobile_con_stat);
        TextView wifiStatTv = (TextView) findViewById(R.id.text_wifi_stat);
        TextView wifiContStatTv = (TextView) findViewById(R.id.text_wifi_con_stat);
        TextView gpsStatTv = (TextView) findViewById(R.id.text_gps_stat);
        TextView netStatTv = (TextView) findViewById(R.id.text_net_stat);
        
        Map<String, Boolean> comStatMap = CmnUtils.getComInfo((ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE));
        
        if (comStatMap.get(CmnConsts.MOBILE_STAT)) {
            mobileStatTv.setTextColor(Color.GREEN);
            mobileStatTv.setText(CmnConsts.STAT_AVAIL);
        } else {
            mobileStatTv.setTextColor(Color.RED);
            mobileStatTv.setText(CmnConsts.STAT_NOT_AVAIL);
        }
        if (comStatMap.get(CmnConsts.MOBILE_CON)) {
            mobileConStatTv.setTextColor(Color.GREEN);
            mobileConStatTv.setText(CmnConsts.STAT_CONN);
            isMobileCon = true;
        } else {
            mobileConStatTv.setTextColor(Color.RED);
            mobileConStatTv.setText(CmnConsts.STAT_NOT_CONN);
            isMobileCon = false;
        }
        
        if (comStatMap.get(CmnConsts.WIFI_STAT)) {
            wifiStatTv.setTextColor(Color.GREEN);
            wifiStatTv.setText(CmnConsts.STAT_AVAIL);
        } else {
            wifiStatTv.setTextColor(Color.RED);
            wifiStatTv.setText(CmnConsts.STAT_NOT_AVAIL);
        }
        if (comStatMap.get(CmnConsts.WIFI_STAT)) {
            wifiContStatTv.setTextColor(Color.GREEN);
            wifiContStatTv.setText(CmnConsts.STAT_CONN);
            isWifiCon = true;
        } else {
            wifiContStatTv.setTextColor(Color.RED);
            wifiContStatTv.setText(CmnConsts.STAT_NOT_CONN);
            isWifiCon = false;
        }
        
        Map<String, Boolean> locStatMap = CmnUtils.getLocInfo((LocationManager) getSystemService(Context.LOCATION_SERVICE));
        if (locStatMap.get(CmnConsts.GPS_STAT)) {
            gpsStatTv.setTextColor(Color.GREEN);
            gpsStatTv.setText(CmnConsts.STAT_AVAIL);
            isNetworkLocationActivated = true;
        } else {
            gpsStatTv.setTextColor(Color.RED);
            gpsStatTv.setText(CmnConsts.STAT_NOT_AVAIL);
            isNetworkLocationActivated = false;
        }
        if (locStatMap.get(CmnConsts.NETWORK_STAT)) {
            netStatTv.setTextColor(Color.GREEN);
            netStatTv.setText(CmnConsts.STAT_AVAIL);
            isGpsLocationActivated = true;
        } else {
            netStatTv.setTextColor(Color.RED);
            netStatTv.setText(CmnConsts.STAT_NOT_AVAIL);
            isGpsLocationActivated = false;
        }
    }
}